/**
 *
 */
package com.angel.io.validators.impl;

import java.lang.reflect.Method;

import com.angel.common.helpers.AnnotationHelper;
import com.angel.io.annotations.RowProcessor;
import com.angel.io.annotations.RowProcessorCommand;
import com.angel.io.exceptions.RowProcessorCommandValidatorException;
import com.angel.io.validators.RowProcessorCommandValidator;

/**
 * @author William
 *
 */
public class ColumnsPositionRowProcessorAnnotationValidator implements RowProcessorCommandValidator{

	public void validate(Class<?> importRowCommandClass) throws RowProcessorCommandValidatorException {
		boolean hasAnnotation = AnnotationHelper.hasAnnotation(importRowCommandClass, RowProcessorCommand.class);
		if(hasAnnotation){
			RowProcessorCommand rowProcessorCommand = AnnotationHelper.getAnnotation(importRowCommandClass, RowProcessorCommand.class);
			Method rowProcessorMethod = AnnotationHelper.getMethodWithAnnotation(importRowCommandClass, RowProcessor.class);
			if(rowProcessorMethod != null){
				RowProcessor rowProcessor = AnnotationHelper.getAnnotation(rowProcessorMethod, RowProcessor.class);
				for(int pos : rowProcessor.columnsParameters()){
					if(pos < 0 || pos - 1 > rowProcessorCommand.columnsRow().length){
						throw new RowProcessorCommandValidatorException("Invalid column number [" + pos + "] for row processor class ["
								+ importRowCommandClass.getName() + "]. It declared [" + rowProcessorCommand.columnsRow().length + "] columns.");
					}
				}
			}
		}
	}


}
